package com.onesports.intelligent.k12.polarlight.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.onesports.intelligent.k12.polarlight.data.permission.CustomDataPermissionInterceptor;
import com.onesports.intelligent.k12.polarlight.data.permission.DataPermissionHandler;
import com.onesports.intelligent.k12.polarlight.data.permission.DataPermissionHandlerImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author aj
 */
@Configuration
public class MybatisPlusConfiguration {

    @Bean
    public DataPermissionHandler dataPermissionHandler() {
        return new DataPermissionHandlerImpl();
    }

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.POSTGRE_SQL);
        // 禁用自动优化
        paginationInnerInterceptor.setOptimizeJoin(false);
        interceptor.addInnerInterceptor(new CustomDataPermissionInterceptor(dataPermissionHandler()));
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        //乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

}
